package com.example.administrator.mynews.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.mynews.R;
import com.example.administrator.mynews.bean.GuoKeDetailBean;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.administrator.mynews.Config.Config.API_GUOKE_GET_DETAILS;

/**
 * Created by Administrator on 2017/5/17.
 */

public class GuoKeNewsActivity extends AppCompatActivity {
    private ImageView imgIcon;
    private TextView tvTitle;
    private WebView webView;
    private WebSettings settings;
    private Handler mHandler=new Handler();
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guoke_news);
        imgIcon= (ImageView) findViewById(R.id.img_guoke_detail_icon);
        tvTitle= (TextView) findViewById(R.id.tv_guoke_detail_title);
        webView= (WebView) findViewById(R.id.tv_guoke_detail_content);
        initData();
        initWebView();
        initDialog();
    }

    private void initDialog() {
        progressDialog = ProgressDialog.show(this, "提示", "正在努力加载中", true, false);
    }

    private void initWebView() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int scale = dm.densityDpi;
        if (scale == 240) { //
            webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (scale == 160) {
            webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        } else {
            webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
        }
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setInitialScale(70);
        webView.setHorizontalScrollbarOverlay(true);
        webView.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
    }

    private void initData() {
        OkHttpClient mOkHttpClient=new OkHttpClient();
        String url=API_GUOKE_GET_DETAILS.replace("{id}",getIntent().getIntExtra("id",0)+"");
        Log.d("xu","url=="+url);
        Request.Builder requestBuilder = new Request.Builder().url(url);
        //可以省略，默认是GET请求
        requestBuilder.method("GET",null);
        Request request = requestBuilder.build();
        Call mcall= mOkHttpClient.newCall(request);
        mcall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("xu","访问网络出错");
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String content=response.body().string();
                    Log.d("qin","content=="+content);
                    final GuoKeDetailBean guoKeDetailBean=new Gson().fromJson(content,GuoKeDetailBean.class);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(GuoKeNewsActivity.this)
                                    .load(guoKeDetailBean.getResult().getSmall_image())
                                    .into(imgIcon);
                            tvTitle.setText(guoKeDetailBean.getResult().getTitle());
                            webView.loadDataWithBaseURL(null, guoKeDetailBean.getResult().getContent(), "text/html", "utf-8", null);
                            webView.getSettings().setJavaScriptEnabled(true);
                            webView.setWebChromeClient(new WebChromeClient());
                        }
                    });

                }catch (Exception e){
                    Log.d("xu","信息解析错误"+e.getMessage());
                }
                progressDialog.dismiss();
            }
        });
    }
}


